package com.atguigu;

import com.atguigu.bean.Person;
import com.atguigu.config.MainConfig;
import com.atguigu.config.MainConfig2;
import com.atguigu.config.MainConfig3;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.env.Environment;

import java.util.Map;

public class MainTest {


    @Test
    public void test1() {
        //使用配置文件执行方式
        ApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("beans.xml");
        Person person = classPathXmlApplicationContext.getBean("person", Person.class);

        System.out.println(person);
    }


    @Test
    public void test2() {

        //使用配置类执行方式
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
        Person person = applicationContext.getBean(Person.class);
        System.out.println(person);


        //根据方法名也就是id进行bean选择
        //获取指定配置类的id值
        String[] beanNamesForType = applicationContext.getBeanNamesForType(Person.class);
        for (String name : beanNamesForType) {
            //这里的name就是id
            System.out.println(name);
        }

    }


    @Test
    public void test3() {

        //使用配置类执行方式
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig2.class);
        //测试懒加载,单例模式下,第一次获取时,才创建对象
        Person person = applicationContext.getBean(Person.class);
        System.out.println("IOC容器创建完成.....");

    }


    @Test
    public void test4() {

        //使用配置类执行方式
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig3.class);
        //动态获取环境变量的值: Windows 10
        Environment environment = applicationContext.getEnvironment();
        //获取操作系统的名称
        String property = environment.getProperty("os.name");

        System.out.println(property);
        String[] beanNamesForType = applicationContext.getBeanNamesForType(Person.class);
        for (String string : beanNamesForType) {
            System.out.println(string);
        }

        Map<String, Person> beansOfType = applicationContext.getBeansOfType(Person.class);
        System.out.println(beansOfType);

    }

}
